// PROGRAMA : CTLPC001
// BIBLIOTECA : PRODETR
// SISTEMA : INFORMACOES DE SEGURANCA PUBLICA - SISP
// CLIENTE : SEJUSP - SEC. DA JUSTICA E SEGURANCA PUBLICA
// INSTITUTO DE IDENTIFICACAO, DETRAN E SEFAZ
// PROGRAMADOR : EDU/FM DATA: 27/05/92
// OBJETIVO : CONSULTA DADOS ARQ. CONTROLE. OPCAO: 'CEC', 'CEN'
// ATUALIZACOES DATA
// BIBLIOTECA : SISP
// OBJETIVO : ADAPTACAO PARA O SHOPPING DO CIDADAO (P200)
// : ALTERACAO DE TELA 28/09/1999
// : ALTERACAO DE DATA N6 PARA N8 04/10/1999
// OBS : ESTE PROGRAMA FOI REESTRUTURADO POR SOLON MIRANDA

package br.com.vti.sisp_sicrim.antigo.controller.programa;

import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Ctlpc001A;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Ctltc001;
import br.com.vti.sisp_sicrim.antigo.model.SicControle;
import br.com.vti.sisp_sicrim.antigo.model.SicTabelas;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.Sisgsisp;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Ctlpc001 OBJETIVO : CONSULTA DADOS ARQ. CONTROLE. OPCAO: 'CEC', 'CEN' *
 * 
 * @author Incode - Data: mai/2014
 * 
 */

@Resource
@Path("/antigo/ctlpc001")
public class Ctlpc001 {
	// Estado Anterior
	Ctlpc001 ctlpc001Anterior;
	// Views utilizadas
	SicControle vwDvecon = new SicControle();
	SicTabelas vwTabele = new SicTabelas();
	// Telas/relatorios utilizados
	Ctlpc001A ctlpc001A;
	Ctltc001 ctltc001;
	// Includes utilizados
	// Areas globais / session
	Sisgsisp sisgsisp; // Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();

	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	private int i;
	private int j;
	// private String conf;
	// private String nomfun;
	// private String nomeClasse = this.getClass().getSimpleName();
	// private String tipdoc;
	private long ndemid;
	private String ndemidstr;
	// private String chave;
	// private int datemi;
	private long valpago;
	// private int dataCorrente;
	private int nroReg = 1;
	private String faseproc = "0"; // SETA PF'S

	private Ctlpc001A preMapas() {
		ctlpc001A = new Ctlpc001A(true, sessao);
		ctltc001 = new Ctltc001(true, sessao);
		ctlpc001A.setDataCorrente(UtilBean.obtemDataDia());
		ctlpc001A.setNomeClasse(this.getClass().getSimpleName().toUpperCase());
		ctltc001.setDataCorrente(UtilBean.obtemDataDia());
		ctltc001.setNomeClasse(this.getClass().getSimpleName().toUpperCase());

		return ctlpc001A;
	}

	public Ctlpc001(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public void executar(Ctlpc001A ctlpc001A, Ctltc001 ctltc001) {
		try {
			main(ctlpc001A, ctltc001);
		} catch (Exception e) {
			ExceptionProgram excp = new ExceptionProgram();
			excp.trataErroSiscerro(e, sessao, this.getClass().getSimpleName());
		}
	}

	private void main(Ctlpc001A pctlpc001A, Ctltc001 pctltc001) throws Exception {
		if (pctlpc001A == null && pctltc001 == null) {
			primeiraVez = true;
			// ctlpc001A = new Ctlpc001A (primeiraVez);
			ctlpc001A = preMapas();
			ctlpc001A.setNavegar("../ctlpc001/executar");
			result.include("ctlpc001A", ctlpc001A);
			ctlpc001A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			sessao.setAtributo("CTLPC001", this);
			return;
		} else {
			restaurarEstados(pctlpc001A, pctltc001);
		}

		switch (faseproc) {
		case "0": {
			ctltc001.setConf("S");
			ctlpc001A.receberTela(false, result);
			if (!UtilBean.funcao.equals(UtilBean.ctt_processa)) {
				ctlpc001A.enviarMensagem(null, "");
				sessao.setAtributo("CTLPC001", this);
				result.include("ctlpc001A", ctlpc001A);
				ctlpc001A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			switch (ctlpc001A.getOperacao()) {
			case "CEC": {
				// Par�grafo: r1()
				if (!ctlpc001A.getChave().trim().equals("")) {
					vwDvecon.consultaNumeroChassi(ctlpc001A.getChave());
					// 164

					if (vwDvecon.totalRegistros() == 0) {
						ctlpc001A.setMensagemSaida("REGISTRO INEXISTENTE");
						sessao.setAtributo("CTLPC001", this);
						result.include("ctlpc001A", ctlpc001A);
						ctlpc001A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
						return;
					}
					vwDvecon.fecha();
				}

				vwDvecon.leituraNumeroChassiMaiorig(ctlpc001A.getChave());
				ctlcc001();

				faseproc = "1";
				sessao.setAtributo("CTLPC001", this);
				result.include("ctltc001", ctltc001);
				ctltc001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			case "CEN": {
				// Par�grafo: r2()

				if (!ctlpc001A.getChave().trim().equals("")) {
					j = 13;
					ndemid = 0;
					ndemidstr = String.valueOf(ndemid);
					while (ndemidstr.length() < 13)
						ndemidstr = "0" + ndemidstr;
					for (i = 12; i >= 0; i--) {
						if (UtilBean.validarMascara(UtilBean.subStr(ctlpc001A.getChave(), i, 1), "N")) {
							ndemidstr = UtilBean.moveSubstring(ndemidstr, j - 1, 1, "N", UtilBean.subStr(ctlpc001A.getChave(), i, 1));
							j -= 1;
						}

					}

					ndemid = Long.parseLong(ndemidstr);
					vwDvecon.consultaNumeroDocumento(ndemid);
					if (vwDvecon.totalRegistros() == 0) {
						ctlpc001A.setMensagemSaida("REGISTRO INEXISTENTE");
						sessao.setAtributo("CTLPC001", this);
						result.include("ctlpc001A", ctlpc001A);
						ctlpc001A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
						break;
					}
					vwDvecon.fecha();
				}

				vwDvecon.leituraNumeroDocumentoMaiorig(ndemid);
				ctlcc001();

				faseproc = "1";
				sessao.setAtributo("CTLPC001", this);
				result.include("ctltc001", ctltc001);
				ctltc001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			default: {
				testar();
				return;
			}
			}
			return;
		}
		case "1": {
			ctltc001.receberTela(false, result);
			if (!(ctltc001.getOperacao().equals("CEC") || ctltc001.getOperacao().equals("CEN") || ctltc001.getOperacao().equals("CED") || ctltc001.getOperacao().equals("CDC") || ctltc001.getOperacao().equals("CDD") || ctltc001.getOperacao().equals("ELD"))) {
				testar();
				return;
			}

			if (ctltc001.getConf().equals("N")) {
				faseproc = "0";
				ctlpc001A.setMensagem("");
				sessao.setAtributo("CTLPC001", this);
				result.include("ctlpc001A", ctlpc001A);
				ctlpc001A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}

			if (!ctltc001.getChave().trim().equals("")) {
				if (!ctlpc001A.getChave().trim().equals(ctltc001.getChave().trim())) {
					ctlpc001A.setChave(ctltc001.getChave());
				}
				if (ctltc001.getOperacao().equals("CEC")) {
					vwDvecon.leituraNumeroChassiMaiorig(ctltc001.getChave());
				} else {
					vwDvecon.leituraNumeroDocumentoMaiorig(Long.parseLong(ctltc001.getChave()));
				}
				ctlcc001();
				faseproc = "1";
				sessao.setAtributo("CTLPC001", this);
				result.include("ctltc001", ctltc001);
				ctltc001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}

			ctltc001.setTipdoc("");
			// INCLUDE CTLCCA02

			if (ctltc001.getChave().trim().equals("")) {
				faseproc = "0";
				sessao.setAtributo("CTLPC001", this);
				result.include("ctlpc001A", ctlpc001A);
				ctlpc001A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			faseproc = "0";
			break;
		}
		}

		sessao.setAtributo("CTLPC001", this);
		result.include("ctlpc001A", ctlpc001A);
		ctlpc001A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
	}

	private void testar() {
		funcoes.sissm001(result, sessao);
	}

	private boolean ctlcc001() throws Exception {
		ctltc001.setDatemi(vwDvecon.getDataEmissao());
		// INVERTE DATA
		ctltc001.setDatemi(funcoes.sissd002(ctltc001.getDatemi()));

		valpago = vwDvecon.getValorPago();
		vwTabele.pesq = UtilBean.moveSubstring(vwTabele.pesq, 0, 3, "N", 18);
		vwTabele.pesq = UtilBean.moveSubstring(vwTabele.pesq, 3, 12, "N", vwDvecon.getMatriculaFunc());
		// PESQUISA TABELA
		vwTabele.resp = funcoes.sisst001(18, vwDvecon.getMatriculaFunc(), vwTabele.resp);

		ctltc001.setNomfun(vwTabele.resp);
		vwTabele.pesq = UtilBean.moveSubstring(vwTabele.pesq, 0, 3, "N", 19);
		vwTabele.pesq = UtilBean.moveSubstring(vwTabele.pesq, 3, 12, "N", vwDvecon.getTipoDocumento());
		// PESQUISA TABELA
		vwTabele.chalfa = funcoes.sisst002(19, vwDvecon.getTipoDocumento(), vwTabele.chalfa);

		ctltc001.setTipdoc(vwTabele.chalfa);
		ctltc001.setChave(ctlpc001A.getChave());
		ctltc001.setSiccontrole(vwDvecon);

		return true;
	}

	private void restaurarEstados(Ctlpc001A pctlpc001A, Ctltc001 pctltc001) {
		this.ctlpc001Anterior = (Ctlpc001) sessao.getAtributo("CTLPC001");
		if (pctlpc001A != null) {
			ctlpc001A = pctlpc001A;
		} else {
			ctlpc001A = this.ctlpc001Anterior.ctlpc001A;
		}
		ctlpc001A.setSessao(sessao);
		if (pctltc001 != null) {
			ctltc001 = pctltc001;
		} else {
			ctltc001 = this.ctlpc001Anterior.ctltc001;
		}
		ctltc001.setSessao(sessao);
		this.faseproc = this.ctlpc001Anterior.faseproc;
		this.vwDvecon = this.ctlpc001Anterior.vwDvecon;

	}
}
